package com.discuss.community.controller;

import com.discuss.community.annotaion.LoginRequired;
import com.discuss.community.entity.Event;
import com.discuss.community.entity.Page;
import com.discuss.community.entity.User;
import com.discuss.community.event.EventProducer;
import com.discuss.community.service.FollowService;
import com.discuss.community.service.UserService;
import com.discuss.community.util.CommunityConstant;
import com.discuss.community.util.CommunityUtil;
import com.discuss.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant {

    @Autowired
    private FollowService followService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserService userService;
    @Autowired
    private EventProducer producer;

    @PostMapping("/follow")
    @ResponseBody
    @LoginRequired
    public String follow(int entityType, int entityId) {
        User user = hostHolder.getUser();

        followService.follow(user.getId(), entityType, entityId);

        // 触发关注事件
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(user.getId())
                .setEntityId(entityId)
                .setEntityType(entityType)
                .setEntityUserId(entityId);

        producer.fireEvent(event);

        return CommunityUtil.getJsonString(0, "已关注");
    }

    @PostMapping("/unfollow")
    @ResponseBody
    @LoginRequired
    public String unFollow(int entityType, int entityId) {
        User user = hostHolder.getUser();

        followService.unFollow(user.getId(), entityType, entityId);
        return CommunityUtil.getJsonString(0 , "已经取消关注");
    }

    @GetMapping("/followees/{userId}")
    public String getFollowee(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.getUserByUserId(userId);
        if(user == null) {
            throw  new RuntimeException("用户不存在");
        }

        model.addAttribute("user", user);
        page.setLimit(5);
        page.setPath("/followees/" + userId);
        page.setRows((int) followService.findFolloweeCount(userId, ENTITY_TYPE_USER));

        List<Map<String, Object>> userList = followService.findFollowee(userId, page.getOffset(), page.getLimit());

        for (Map<String, Object> map : userList) {
            User u = (User)map.get("user");
            map.put("hasFollowed", hasFollowed(userId));
        }

        model.addAttribute("users", userList);
        return "/site/followee";
    }

    @GetMapping("/followers/{userId}")
    public String getFollower(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.getUserByUserId(userId);
        if(user == null) {
            throw  new RuntimeException("用户不存在");
        }

        model.addAttribute("user", user);
        page.setLimit(5);
        page.setPath("/followers/" + userId);
        page.setRows((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));

        List<Map<String, Object>> userList = followService.findFollower(userId, page.getOffset(), page.getLimit());

        for (Map<String, Object> map : userList) {
            User u = (User)map.get("user");
            map.put("hasFollowed", hasFollowed(userId));
        }

        model.addAttribute("users", userList);
        return "/site/follower";
    }

    private boolean hasFollowed(int userId) {

        User user = hostHolder.getUser();
        if(user == null) {
            return false;
        }

        return followService.isFollowed(user.getId(), ENTITY_TYPE_USER, userId);
    }

}
